package com.offcn.spark.p3

import org.apache.spark.{SparkConf, SparkContext}

/**
 * @Auther: BigData-LGW
 * @ClassName: ReduceBy
 * @Date: 2020/12/7 19:48
 * @功能描述: $FunctionDescription
 * @Version:1.0
 */
object ReduceBy {
    def main(args: Array[String]): Unit = {
        val conf = new SparkConf()
            .setAppName("ReduceBy")
            .setMaster("local[*]")
        val sc = new SparkContext(conf)
        reduceBy(sc)
        sc.stop()
    }

    def reduceBy(sc: SparkContext) = {
        case class Student(id:Int,name:String,province:String)
        val stuRDD = sc.parallelize(List(
            Student(1, "白普州", "安徽"),
            Student(2, "伍齐城", "山东"),
            Student(3, "曹佳", "甘肃"),
            Student(4, "姚远", "甘肃"),
            Student(5, "匿名大哥", "黑吉辽"),
            Student(10086, "欧阳龙生", "黑吉辽")
        ))
        val ret = stuRDD.map(stu => (stu.province,1)).reduceByKey((v1,v2) => v1 +v2)
        ret.foreach{
            case (province,count) => {
                println(s"province: ${province}, count: ${count}")
            }
        }}
}
